package com.ymatou.iapi.fundcommission.testcase;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import oracle.net.aso.a;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.fundcommission.service.DailyReportGetCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.database.sqlwapper.YmtFundDimensionWapper;
import com.ymttest.utils.JsonBeanHelper;
/**
*资金后台获取佣金日账单接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_DailyReportGet{
	private static DailyReportGetCall dailyreportgetCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("资金后台获取佣金日账单接口");
	}

	@Before
	public void caseUp() {
		dailyreportgetCall=new DailyReportGetCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(dailyreportgetCall.getOpurl());
	}

	/**
	 * 接口返回信息公用错误信息部分断言（正向Case场景）
	 * @throws Exception
	 */	
	public static void dailyreportgetreturncode() throws Exception {
	    try {	    	
			//获取返回报文的数据
			String dataStr = dailyreportgetCall.getReturnData();
			//返回报文断言
			Logger.verifyNotNull(dataStr, "返回报文非空");			
			String Success =  new JSONObject(dataStr).getString("Success");
			Logger.verifyIsContains(true, Success, "\"Success\":true");
			String errMsg =  new JSONObject(dataStr).getString("ErrMsg");
			Logger.verifyIsNull(errMsg, "Msg等于空");
			String errCode =  new JSONObject(dataStr).getString("ErrCode");
			Logger.verifyIsNull(errCode, "ErrCode等于空");
			String body =  new JSONObject(dataStr).getString("Body");
			Logger.verifyIsNull(body, "Body等于空");
			String requestUrl =  new JSONObject(dataStr).getString("RequestUrl");
			Logger.verifyIsNull(requestUrl, "RequestUrl等于空");
	    	    	    	
	    }catch (Exception e) {  
	        e.printStackTrace();
	    }  
	}	
	
	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DailyReportGet_001() {
		Logger.start(true,"资金后台获取佣金日账单（收佣）");
		try {
			//TODO 补全case代码
			//SQL取出Ymt_CommissionCheck 表中第一条数据
			YmtFundDimensionWapper ymtFundDimensionWapper = new YmtFundDimensionWapper();
	    	Map mnull1 = new HashMap();
			List<Map> getseller = ymtFundDimensionWapper.commissiongetsellerid(mnull1);
			int sellerid= (Integer) getseller.get(0).get("SellerId");	//取出卖家id
			String sellerloginid = (String) getseller.get(0).get("SellerLoginId");	//取出卖家用户名
			String betime = (String) getseller.get(0).get("BusinessTime");	//取出业务发生时间

			Map<String,Object> aa = new HashMap<String, Object>();
			aa.put("BeginTime", betime);		
			aa.put("EndTime", betime);		
			aa.put("UserId", sellerid);	
			aa.put("UserLoginId", sellerloginid);			
			//调用接口json文件
			JSONObject dailyreportgetBean =  JsonBeanHelper.getJsonBean("/com.ymatou.iapi.fundcommission/DailyReportGet.json");
			dailyreportgetBean = JsonBeanHelper.updateJsonBean(dailyreportgetBean, aa);	
			
			dailyreportgetCall.setData(dailyreportgetBean);
			dailyreportgetCall.callService();		
			
			//SQL查询收佣费率
	    	Map m2 = new HashMap();
	        m2.put("sellerid", sellerid);
	        m2.put("beginTime", betime);
	        m2.put("endTime", betime);
	        List<Map> commissionamountbysql = ymtFundDimensionWapper.commissionbysellerid(m2);
	        //断言
			dailyreportgetreturncode();
			String dataStr = dailyreportgetCall.getReturnData();	
	        //收佣总费用断言
	        BigDecimal subCommission = ((BigDecimal) commissionamountbysql.get(0).get("SubCommission")).setScale(2,BigDecimal.ROUND_HALF_UP);	//sql查询结果
	        Double tsc = new JSONObject(dataStr).getJSONArray("DailyReportList").getJSONObject(0).getDouble("CommissionAmount");			//取报文结果
	        BigDecimal totalSubCommission = new BigDecimal(tsc).setScale(2,BigDecimal.ROUND_HALF_UP);
	        Logger.verifyEquals(subCommission, totalSubCommission, "收佣费用对比");
	        //Response断言
			int commissionType = (int) new JSONObject(dataStr).getJSONArray("DailyReportList").getJSONObject(0).getDouble("CommissionType");
			Logger.verifyEquals(1, commissionType, "收佣类型对比");
			String sellerLoginIdbyResponse = (String) new JSONObject(dataStr).getJSONArray("DailyReportList").getJSONObject(0).getString("SellerLoginId");
			Logger.verifyEquals(sellerloginid, sellerLoginIdbyResponse, "卖家用户名对比");
			int sellerUserIdbyResponse = (Integer) new JSONObject(dataStr).getJSONArray("DailyReportList").getJSONObject(0).getInt("SellerUserId");
			Logger.verifyEquals(sellerid, sellerUserIdbyResponse, "卖家id对比");			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DailyReportGet_002() {
		Logger.start(true,"资金后台获取佣金日账单（退佣）");
		try {
			//TODO 补全case代码
			//SQL取出Ymt_CommissionCheck 表中第一条数据
			YmtFundDimensionWapper ymtFundDimensionWapper = new YmtFundDimensionWapper();
	    	Map mnull1 = new HashMap();
			List<Map> getseller = ymtFundDimensionWapper.commissiongetsellerid(mnull1);
			int sellerid= (Integer) getseller.get(0).get("SellerId");	//取出卖家id
			String sellerloginid = (String) getseller.get(0).get("SellerLoginId");	//取出卖家用户名
			String betime = (String) getseller.get(0).get("BusinessTime");	//取出业务发生时间

			Map<String,Object> aa = new HashMap<String, Object>();
			aa.put("CommissionType", "2");		//佣金类型 1扣佣 2退佣 3返佣
			aa.put("BeginTime", betime);		
			aa.put("EndTime", betime);		
			aa.put("UserId", sellerid);	
			aa.put("UserLoginId", sellerloginid);			
			//调用接口json文件
			JSONObject dailyreportgetBean =  JsonBeanHelper.getJsonBean("/com.ymatou.iapi.fundcommission/DailyReportGet.json");
			dailyreportgetBean = JsonBeanHelper.updateJsonBean(dailyreportgetBean, aa);	
			
			dailyreportgetCall.setData(dailyreportgetBean);
			dailyreportgetCall.callService();		
			
			//SQL查询收佣费率
	    	Map m2 = new HashMap();
	        m2.put("sellerid", sellerid);
	        m2.put("beginTime", betime);
	        m2.put("endTime", betime);
	        List<Map> commissionamountbysql = ymtFundDimensionWapper.commissionbysellerid(m2);
	        //断言
			dailyreportgetreturncode();
			String dataStr = dailyreportgetCall.getReturnData();	
	        //退佣总费用断言
	        BigDecimal returnCommission = ((BigDecimal) commissionamountbysql.get(0).get("ReturnCommission")).setScale(2,BigDecimal.ROUND_HALF_UP);//sql查询结果
	        Double rtc = new JSONObject(dataStr).getJSONArray("DailyReportList").getJSONObject(0).getDouble("CommissionAmount");//取报文结果
	        BigDecimal totalReturnCommission = new BigDecimal(rtc).setScale(2,BigDecimal.ROUND_HALF_UP);
	        Logger.verifyEquals(returnCommission, totalReturnCommission, "总退佣费用对比");
	        //Response断言
			int commissionType = (int) new JSONObject(dataStr).getJSONArray("DailyReportList").getJSONObject(0).getDouble("CommissionType");
			Logger.verifyEquals(2, commissionType, "退佣类型对比");
			String sellerLoginIdbyResponse = (String) new JSONObject(dataStr).getJSONArray("DailyReportList").getJSONObject(0).getString("SellerLoginId");
			Logger.verifyEquals(sellerloginid, sellerLoginIdbyResponse, "卖家用户名对比");
			int sellerUserIdbyResponse = (Integer) new JSONObject(dataStr).getJSONArray("DailyReportList").getJSONObject(0).getInt("SellerUserId");
			Logger.verifyEquals(sellerid, sellerUserIdbyResponse, "卖家id对比");			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DailyReportGet_003() {
		Logger.start(true,"资金后台获取佣金日账单（返佣）");
		try {
			//TODO 补全case代码
			//SQL取出Ymt_CommissionCheck 表中第一条数据
			YmtFundDimensionWapper ymtFundDimensionWapper = new YmtFundDimensionWapper();
	    	Map mnull1 = new HashMap();
			List<Map> getseller = ymtFundDimensionWapper.commissiongetsellerid(mnull1);
			int sellerid= (Integer) getseller.get(0).get("SellerId");	//取出卖家id
			String sellerloginid = (String) getseller.get(0).get("SellerLoginId");	//取出卖家用户名
			String betime = (String) getseller.get(0).get("BusinessTime");	//取出业务发生时间

			Map<String,Object> aa = new HashMap<String, Object>();
			aa.put("CommissionType", "3");		//佣金类型 1扣佣 2退佣 3返佣
			aa.put("BeginTime", betime);		
			aa.put("EndTime", betime);		
			aa.put("UserId", sellerid);	
			aa.put("UserLoginId", sellerloginid);			
			//调用接口json文件
			JSONObject dailyreportgetBean =  JsonBeanHelper.getJsonBean("/com.ymatou.iapi.fundcommission/DailyReportGet.json");
			dailyreportgetBean = JsonBeanHelper.updateJsonBean(dailyreportgetBean, aa);	
			
			dailyreportgetCall.setData(dailyreportgetBean);
			dailyreportgetCall.callService();		
			
			//SQL查询收佣费率
	    	Map m2 = new HashMap();
	        m2.put("sellerid", sellerid);
	        m2.put("beginTime", betime);
	        m2.put("endTime", betime);
	        List<Map> commissionamountbysql = ymtFundDimensionWapper.commissionbysellerid(m2);
	        //断言
			dailyreportgetreturncode();
			String dataStr = dailyreportgetCall.getReturnData();	
	        //返佣总费用断言
	        BigDecimal returnCommission = ((BigDecimal) commissionamountbysql.get(0).get("RefundCommission")).setScale(2,BigDecimal.ROUND_HALF_UP);//sql查询结果
	        Double rtc = new JSONObject(dataStr).getJSONArray("DailyReportList").getJSONObject(0).getDouble("CommissionAmount");//取报文结果
	        BigDecimal totalReturnCommission = new BigDecimal(rtc).setScale(2,BigDecimal.ROUND_HALF_UP);
	        Logger.verifyEquals(returnCommission, totalReturnCommission, "总返佣费用对比");
	        //Response断言
			int commissionType = (int) new JSONObject(dataStr).getJSONArray("DailyReportList").getJSONObject(0).getDouble("CommissionType");
			Logger.verifyEquals(3, commissionType, "返佣类型对比");
			String sellerLoginIdbyResponse = (String) new JSONObject(dataStr).getJSONArray("DailyReportList").getJSONObject(0).getString("SellerLoginId");
			Logger.verifyEquals(sellerloginid, sellerLoginIdbyResponse, "卖家用户名对比");
			int sellerUserIdbyResponse = (Integer) new JSONObject(dataStr).getJSONArray("DailyReportList").getJSONObject(0).getInt("SellerUserId");
			Logger.verifyEquals(sellerid, sellerUserIdbyResponse, "卖家id对比");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DailyReportGet_004() {
		Logger.start(false,"当IsPaged为true时");
		try {
			//TODO 补全case代码
			//SQL取出Ymt_CommissionCheck 表中第一条数据
			YmtFundDimensionWapper ymtFundDimensionWapper = new YmtFundDimensionWapper();
	    	Map mnull1 = new HashMap();
			List<Map> getseller = ymtFundDimensionWapper.commissiongetsellerid(mnull1);
			int sellerid= (Integer) getseller.get(0).get("SellerId");	//取出卖家id
			String sellerloginid = (String) getseller.get(0).get("SellerLoginId");	//取出卖家用户名
			String betime = (String) getseller.get(0).get("BusinessTime");	//取出业务发生时间

			Map<String,Object> aa = new HashMap<String, Object>();
			aa.put("BeginTime", betime);		
			aa.put("EndTime", betime);		
			aa.put("UserId", sellerid);	
			aa.put("UserLoginId", sellerloginid);		
			aa.put("IsPaged", true);
			//调用接口json文件
			JSONObject dailyreportgetBean =  JsonBeanHelper.getJsonBean("/com.ymatou.iapi.fundcommission/DailyReportGet.json");
			dailyreportgetBean = JsonBeanHelper.updateJsonBean(dailyreportgetBean, aa);	
			
			dailyreportgetCall.setData(dailyreportgetBean);
			dailyreportgetCall.callService();	

			dailyreportgetreturncode();
			String dataStr = dailyreportgetCall.getReturnData();			
			int dailyReportListlength = new JSONObject(dataStr).getJSONArray("DailyReportList").length();
			Logger.verifyEquals(1, dailyReportListlength, "允许分页每页显示数量为1");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DailyReportGet_005() {
		Logger.start(false,"当IsPaged为false时");
		try {
			//TODO 补全case代码
			//SQL取出Ymt_CommissionCheck 表中第一条数据
			YmtFundDimensionWapper ymtFundDimensionWapper = new YmtFundDimensionWapper();
	    	Map mnull1 = new HashMap();
			List<Map> getseller = ymtFundDimensionWapper.commissiongetsellerid(mnull1);
			int sellerid= (Integer) getseller.get(0).get("SellerId");	//取出卖家id
			String sellerloginid = (String) getseller.get(0).get("SellerLoginId");	//取出卖家用户名
			String betime = (String) getseller.get(0).get("BusinessTime");	//取出业务发生时间

			Map<String,Object> aa = new HashMap<String, Object>();
			aa.put("BeginTime", betime);		
			aa.put("EndTime", betime);		
			aa.put("UserId", sellerid);	
			aa.put("UserLoginId", sellerloginid);		
			aa.put("IsPaged", false);
			aa.put("PageSize", 3);
			//调用接口json文件
			JSONObject dailyreportgetBean =  JsonBeanHelper.getJsonBean("/com.ymatou.iapi.fundcommission/DailyReportGet.json");
			dailyreportgetBean = JsonBeanHelper.updateJsonBean(dailyreportgetBean, aa);	
			
			dailyreportgetCall.setData(dailyreportgetBean);
			dailyreportgetCall.callService();	

			dailyreportgetreturncode();
			String dataStr = dailyreportgetCall.getReturnData();			
			int dailyReportListlength = new JSONObject(dataStr).getJSONArray("DailyReportList").length();
			Logger.verifyEquals(3, dailyReportListlength, "不允许分页");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
}